import axios from '../../../utils/http'
import XLSX from 'xlsx'
import Cookies from 'js-cookie'
import {baseURL} from '../../../utils/http'
// 查询学期
export async function getTermList() {
  const {
    data
  } = await axios.get('/bac/term/getTermList')
  return data
}

//查询
export async function getBuildList() {
  const {
    data
  } = await axios.get('/bac/building/BuildList')
  return data
}

// 查询周次
export async function getWeekList(parameter) {
  const {
    data
  } = await axios.get('/bac/weekDate/getWeekListByTerm?termId=' + parameter)
  return data
}


// 获取辅导员值班表
export async function getDutyList(parameter) {
  const {
    data
  } = await axios.get('/dormNight/counselorPlan' + parameter)
  return data
}

// 修改 值班情况  /iseSchedule/schedule
export async function updataDutyDetails(id, remark, teacherName) {
  const {
    data
  } = await axios.put('/dormNight/counselorPlan', {
    id: id,
    remark: remark,
    teacherName: teacherName,
  })
  return data
}

// 根据学期周次获取周次开始时间和结束时间
export async function getWeekTime(termId, week) {
  return axios.get('/bac/weekDate/getTimeByTermWeek', {
    params: {
      termId: termId,
      week: week
    }
  })
}

// 删除值班情况
export async function deleteDutyDetails(data) {
  return axios.delete('/dormNight/counselorPlan', data)
}

// 添加 值班情况
export async function addDutyDetails(data) {
  return axios.post('/dormNight/counselorPlan', data)

}
// 查询分配
export async function getDetailList(parameter) {
  const {
    data
  } = await axios.get('/dormNight/counselorPlan/getPlanDetailList'+parameter)
  return data
}

// Excel导入数据处理
export function excel(file) {
  return new Promise((resolve, reject) => {
    const reader = new FileReader()
    reader.readAsArrayBuffer(file)
    reader.onerror = e => {
      reject(e)
    }
    reader.onload = e => {
      const excelData = e.target.result
      const wb = XLSX.read(excelData, {
        type: 'array'
      })
      const firstSheetName = wb.SheetNames[0]
      const worksheet = wb.Sheets[firstSheetName]
      const columns = processHeader(worksheet)
      const tableData = XLSX.utils.sheet_to_json(columns)
      //可能是文字不符合
      // if (!tableData[0].学期 && !tableData[0].周次) {
      //   resolve(false)
      // }
      resolve(tableData)
    }
  })


}

// 格式化Excel数据 表头
function processHeader(sheet) {
  const range = XLSX.utils.decode_range(sheet['!ref'])
  const C = range.e.c
  for (let i = 0; i <= C; i++) {
    sheet[String.fromCharCode(65 + i) + 1] = sheet[String.fromCharCode(65 + i) + 2]
    delete sheet[String.fromCharCode(65 + i) + 2]
  }
  return sheet
}

// 添加导入Excel数据
export async function addExceldata(excelData) {
  const {
    data
  } = await axios.post('/dormNight/counselorPlan/uploadExcel', excelData)
  return data
}

// 导出Excel数据
export function getExcel() {
  fetch(baseURL+'/dormNight/counselorPlan/downLoadList', {
    method: 'GET',
    headers: {
      'JHC-Authorization': Cookies.get('token')
    },
  })
    .then(res => {
      return res.blob()
    })
    .then(data => {
      const blobUrl = window.URL.createObjectURL(data)
      download(blobUrl)
    })
}
function download(blobUrl) {
  const a = document.createElement('a')
  a.download = '值班计划表' + '.xlsx'
  a.href = blobUrl
  a.click()
}
export function downloadWorkShuttering() {
  fetch(baseURL+'/dormNight/counselorPlan/downLoadTemplate', {
    method: 'GET',
    headers: {
      'JHC-Authorization': Cookies.get('token')
    },
  }).then(res => {
    return res.blob()
  })
    .then(data => {
      const blobUrl = window.URL.createObjectURL(data)
      downloadWork(blobUrl)
    })
}

function downloadWork(blobUrl) {
  const a = document.createElement('a')
  a.download = '值班计划表模板' + '.xlsx'
  a.href = blobUrl
  a.click()
}
